package com.spider.moudle.task;

import com.spider.moudle.constant.Constant;
import com.spider.moudle.entity.Task;
import com.spider.moudle.entity.TaskType;
import com.spider.moudle.entity.WebType;
import com.spider.moudle.util.CenterExecutorUtil;
import com.spider.moudle.util.PropertiesUtil;
import org.eclipse.jetty.util.ConcurrentHashSet;

import java.util.Date;
import java.util.Set;

/**
 * Created by Linbo Hu on 2017/11/30.
 * 言论任务检查工具类(两天内爬过的任务就不爬了)
 */
public class TaskChecker {

    private static TaskChecker instance = new TaskChecker();

    private final Set<Task> set = new ConcurrentHashSet<>();

    private TaskChecker() {

    }

    public static TaskChecker getInstance(){
        return instance;
    }

    /**
     * 判断一个Task是否存在于Set中,如果存在则返回true, 如果不存在则返回false并且将该Task加入到Set中
     * @param task
     * @return
     */
    public Boolean contains(Task task) {
       Boolean exist = set.contains(task);
       if(!exist) {
           set.add(task);
       }
       return exist;
    }

    /**
     * 清除过期的任务
     */
    public void clear() {
        CenterExecutorUtil.execute(() -> {
            while (!Thread.interrupted()) {
                try {
                    Thread.sleep(1000 * 60 * 60 *  Integer.parseInt(PropertiesUtil.getVal("task.check.interval")));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (set) {
                    Date now = new Date();
                    long last_48 = now.getTime() - Constant.LAST_48_HOUR_TIME_STAMP;
                    for(Task task : set) {
                        Date createTime = task.getCreateTime();
                        if(createTime.getTime() < last_48) {
                            set.remove(task);
                        }
                    }
                    System.err.println("清理任务池完成, 当前任务池大小: " + set.size());
                    set.notify();
                }
            }
        });
    }


    public static void main(String[] args) throws InterruptedException {
        TaskChecker checker = TaskChecker.getInstance();
        Date date = new Date();
        int i = 0;
        while (true) {
            String url = "www.baicu.com";
            Task task = new Task(url, TaskType.SPEECH, WebType.FORLUM, "key");
            if (checker.contains(task)) {
                System.out.println(task + "-----存在");
            }
            if(++i > 5000) {
                break;
            }
        }
        checker.clear();

    }









}
